package com.zj.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zj.common.AjaxResult;
import com.zj.ctrl.RegistReturnType;
import com.zj.ctrl.SdkTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class NVRController {

    @Autowired
    private SdkTool sdkTools;

    @RequestMapping("/getFileList")
    public AjaxResult getFileList() throws Exception {
        if (sdkTools.deviceRegist() != 1) return AjaxResult.error("设备注册失败");
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        //
        Date startTime = sdf.parse("2022/09/13 11:20:00");
        Date endTime = sdf.parse("2022/09/13 11:21:00");
        // 获取文件列表
        final List<Map<String, Object>> fileList = sdkTools.getFileList(startTime, endTime, 33);
        return AjaxResult.success(fileList);
    }

    /**
     * 下载文件->文件名
     */
    @RequestMapping("/downloadByfile")
    public int downloadByfile() {
        // 获取文件列表
        int downStatus = sdkTools.downloadByfile("ch0001_00000000700000000");
        System.out.println(downStatus);

        System.out.println("结束....................");
        return downStatus;
    }

    /**
     * 下载文件->时间
     */
    @RequestMapping("/downloadByTime")
    public AjaxResult downloadByTime(String trialNo, String startTime, String endTime, Integer channel, String fileName) throws Exception {
        if (sdkTools.deviceRegist() != 1) return AjaxResult.error("设备注册失败");
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取文件列表
        String path = sdkTools.downloadByTime(trialNo,fileName, channel, sdf.parse(startTime), sdf.parse(endTime));
        return AjaxResult.success(path);
    }
}
